package com.ssm.zy.controller;

import com.ssm.zy.common.controller.BaseController;
import com.ssm.zy.common.result.Result;
import com.ssm.zy.common.result.ResultCode;
import com.ssm.zy.pojo.Comment;
import com.ssm.zy.pojo.Label;
import com.ssm.zy.pojo.Post;
import com.ssm.zy.pojo.vo.CommentVo;
import com.ssm.zy.pojo.vo.PostVo;
import com.ssm.zy.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/post")//帖子
public class PostController extends BaseController {
    @Autowired
    private PostService postService;

    @GetMapping("/check")//查出帖子
    public Result check(@RequestParam(required = false,defaultValue = "1") Integer sort,
                        @RequestParam(required = false,defaultValue = "") Integer labelId,
                        @RequestParam(required = false,defaultValue = "") String likeName){
        Integer userId=super.userId;
        List<PostVo> postList=postService.check(sort,userId,labelId,likeName);
        return new Result(ResultCode.SUCCESS,postList);
    }

    @GetMapping("/viewerCheck")//查出游客观看的帖子
    public Result viewerCheck(@RequestParam(required = false,defaultValue = "1") Integer sort,
                        @RequestParam(required = false,defaultValue = "") Integer labelId,
                        @RequestParam(required = false,defaultValue = "") String likeName){
        List<PostVo> postList=postService.viewerCheck(sort,labelId,likeName);
        return new Result(ResultCode.SUCCESS,postList);
    }
    @GetMapping("checkUserPost")//查找个人发布的帖子
    public Result checkUserPost(){
        Integer userId=super.userId;
        List<PostVo> postVoList =  postService.getcheckUserPost(userId);
        return new Result(ResultCode.SUCCESS,postVoList);
    }

    @GetMapping("checkUserCollectPost")//查找个人收藏的帖子
    public Result checkUserCollectPost(){
        Integer userId=super.userId;
        List<PostVo> postVoList =  postService.getcheckUserCollectPost(userId);
        return new Result(ResultCode.SUCCESS,postVoList);
    }

    @GetMapping("checkUserPraisePost")//查找个人点赞的帖子
    public Result checkUserPraisePost(){
        Integer userId=super.userId;
        List<PostVo> postVoList =  postService.getcheckUserPraisePost(userId);
        return new Result(ResultCode.SUCCESS,postVoList);
    }

    @GetMapping("/commentDetails")//帖子的评论详情
    public Result commentDetails(Integer postId){
        Integer userId=super.userId;
        List<CommentVo> commentList=postService.commentDetails(postId,userId);
        return new Result(ResultCode.SUCCESS,commentList);
    }
    @PostMapping("/commentPost")//评论帖子
    public Result commentPost(@RequestBody Comment comment){
        Integer userId=super.userId;
        int i=postService.commentPost(comment.getPostId(),userId,comment.getCommentContent());
        if (i>0){
            return Result.ok();
        }
        return Result.err();
    }
    @GetMapping("/getLabel")//查找标签
    public Result getLabel(){
        List<Label> labelList = postService.getLabel();
        return new Result(ResultCode.SUCCESS,labelList);
    }
    @PostMapping("/uploadPost")//发布帖子
    public Result uploadPost(@RequestBody PostVo postVo){
        Integer userId=super.userId;
        int i = postService.uploadPost(postVo,userId);
        if (i>0){
            return Result.ok();
        }
        return Result.err();
    }
    @PostMapping("/reportPost")//举报帖子
    public Result reportPost(Integer postId){
        int i=postService.reportPost(postId);
        if (i>0){
            return Result.ok();
        }
        return Result.err();
    }

    @PostMapping("/praisePost")//点赞帖子,如果已经点赞就取消点赞
    public Result praisePost(@RequestBody Post post){
        Integer userId=super.userId;
        int i=postService.addpraisePost(userId,post.getPostId());
        if (i>0){
            return Result.ok();
        }
        return Result.err();
    }

    @PostMapping("/collectPost")//收藏帖子,如果已经收藏就取消收藏
    public Result collectPost(@RequestBody Post post){
        Integer userId=super.userId;
        int i=postService.collectPost(userId,post.getPostId());
        if (i>0){
            return Result.ok();
        }
        return Result.err();
    }

    @PostMapping("/updateUserPost")//修改帖子
    public Result updateUserPost(@RequestBody PostVo postVo){
        Integer userId=super.userId;
        if (postVo.getUserId()==userId){
            int i=postService.updateUserPost(postVo);
            if (i>0){
                return Result.ok();
            }
        }
        return Result.err();
    }

    @GetMapping("/toCheckUserPost")//查找这个人的帖子
    public Result toCheckUserPost(Integer userId){
        List<PostVo> postVoList =  postService.toCheckUserPost(userId);
        return new Result(ResultCode.SUCCESS,postVoList);
    }
    @GetMapping("/toCheckUserCollectPost")//查找这个人收藏的帖子
    public Result toCheckUserCollectPost(Integer userId){
        List<PostVo> postVoList =  postService.getcheckUserCollectPost(userId);
        return new Result(ResultCode.SUCCESS,postVoList);
    }
    @GetMapping("toCheckUserPraisePost")//查找这个人点赞的帖子
    public Result toCheckUserPraisePost(Integer userId){
        List<PostVo> postVoList =  postService.getcheckUserPraisePost(userId);
        return new Result(ResultCode.SUCCESS,postVoList);
    }
}
